package com.yixinsoft.basic.web.controller.manage.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.github.pagehelper.PageInfo;
import com.yixinsoft.basic.common.Constants;
import com.yixinsoft.basic.model.manage.system.Role;
import com.yixinsoft.basic.service.manage.system.ResourceService;
import com.yixinsoft.basic.service.manage.system.RoleService;

/**
 * Created by hqy on 2016/06/05
 */
@Controller
@RequestMapping("/manage/system/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ResourceService resourceService;
	
	@RequiresPermissions("role:view")
	@RequestMapping
	public String list(Model model,
			@RequestParam(required = false, defaultValue = Constants.PAGENUM)Integer p,
			@RequestParam(required = false, defaultValue = Constants.PAGESIZE)Integer ps,
			String roleName) {
		Role role = null;
		if (!StringUtils.isEmpty(roleName)) {
			role = new Role();
			role.setRoleName(roleName);
			model.addAttribute("roleName", roleName);
		}
		PageInfo<Role> pageInfo = roleService.queryListPage(role, p, ps);
		model.addAttribute("pageInfo", pageInfo);
		return "manage/system/role/list";
	}
	
	@RequiresPermissions("role:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(Model model) {
		setCommonData(model);
		model.addAttribute("role", new Role());
		model.addAttribute("op", "新增");
		return "manage/system/role/edit";
	}
	
	@RequiresPermissions("role:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(Model model, Role role, RedirectAttributes redirectAttributes) {
		roleService.save(role);
		redirectAttributes.addFlashAttribute("msg", "新增成功");
		return "redirect:/manage/system/role";
	}
	
	@RequiresPermissions("role:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(Model model, @PathVariable Long id) {
		setCommonData(model);
		model.addAttribute("role", roleService.queryById(id));
		model.addAttribute("op", "修改");
		return "manage/system/role/edit";
	}
	
	@RequiresPermissions("role:update")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
	public String update(Role role, RedirectAttributes redirectAttributes) {
		roleService.update(role);
		redirectAttributes.addFlashAttribute("msg", "修改成功");
		return "redirect:/manage/system/role";
	}
	
	@RequiresPermissions("role:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String delete(Long[] ids, RedirectAttributes redirectAttributes) {
		roleService.deleteByIds(ids);
		redirectAttributes.addFlashAttribute("msg", "删除成功");
		return "redirect:/manage/system/role";
	}
	
	private void setCommonData(Model model) {
		model.addAttribute("resourceList", resourceService.queryList(null));
	}
}
